package com.hongluan.love.ui;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.hongluan.love.Constant;
import com.hongluan.love.R;
import com.hongluan.love.util.FileUtil;
import com.hongluan.love.util.ImageUtil;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.DbUtils;

public class EditUserInfoActivity extends Activity
{
	// activity request code
	protected final int REQUEST_CODE_IMAGE_FAIL = 1;
	protected final int MAX_PHOTO_SHOW = 4;

	private static final int FROM_LOCAL = 0;
	private static final int FROM_CAMERA = 1;
	private static final int FROM_CUT = 2;

	// screenshot
	protected LinearLayout layPhoto;
	protected RelativeLayout layPhotoCapture;
	protected ImageButton imageButton;

	protected String m_photoPath = null;

	protected BitmapUtils m_bitmapUtils;
	protected DbUtils m_dbUtils;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edituserinfo);

		m_bitmapUtils = new BitmapUtils(this);
		m_bitmapUtils.clearCache();

		m_dbUtils = DbUtils.create(this);

		layPhoto = (LinearLayout) findViewById(R.id.layBugImage);
		layPhotoCapture = (RelativeLayout) findViewById(R.id.layBugCapture);
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		loadPhoto();
	}

	protected void loadPhoto()
	{
		layPhoto.removeAllViews();

		LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(1,
				LinearLayout.LayoutParams.MATCH_PARENT, 1);
		layout.gravity = Gravity.LEFT;
		layout.leftMargin = 10;

		if (m_photoPath == null) {
			m_photoPath = FileUtil.getStorePath(this, Constant.PHOTO_PATH);
		}

		List<String> images = FileUtil.getFilePathByExt(m_photoPath,
				Constant.PHOTO_EXT);

		int size = images.size();

		imageButton = new ImageButton(this);
		imageButton.setImageResource(R.drawable.ab_add);
		imageButton.setLayoutParams(layout);
		imageButton.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View view)
			{
				try {
					Intent intent = new Intent(Intent.ACTION_PICK);
					intent.setType("image/*");
					startActivityForResult(intent, FROM_LOCAL);
				} catch (Exception e) {
					Intent contentIntent = new Intent();
					contentIntent.setAction(Intent.ACTION_GET_CONTENT);
					contentIntent.setType("miyuan/*");
					contentIntent.addCategory(Intent.CATEGORY_OPENABLE);
					Intent intent = Intent.createChooser(contentIntent, "选择照片");
					startActivityForResult(intent, REQUEST_CODE_IMAGE_FAIL);
				}
			}
		});
		layPhoto.addView(imageButton); // 添加按钮

		layPhotoCapture.setVisibility(View.VISIBLE);
		for (int i = 0; i < MAX_PHOTO_SHOW; i++) {
			String path = null;
			ImageView imageView = new ImageView(this);
			if (i < size) {
				path = images.get(i);
				imageView.setTag(path);
			}
			m_bitmapUtils.display(imageView, path);
			imageView.setLayoutParams(layout);
			layPhoto.addView(imageView);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		switch (requestCode) {
		case FROM_LOCAL: {
			if (data != null) {
				cutImage(data.getData());
			}
			break;
		}
		case FROM_CUT: {
			Uri uri = data.getData();
			if (saveSelectedPhoto(uri)) {
				Toast.makeText(this, "新增照片成功", Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(this, "新增照片失败, 请重新选择", Toast.LENGTH_SHORT)
						.show();
			}
			break;
		}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	private boolean saveSelectedPhoto(Uri uri)
	{
		boolean res = false;
		ContentResolver resolver = getContentResolver();
		try {
			InputStream stream = resolver.openInputStream(uri);
			Bitmap localBit = BitmapFactory.decodeStream(stream);
			if (localBit != null) {
				String fileName = m_photoPath + "/" + FileUtil.getRandomPath()
						+ "." + Constant.PHOTO_EXT;
				ImageUtil.save(localBit, fileName);
			}
			res = true;
		} catch (FileNotFoundException e) {
		}

		return res;
	}

	private void cutImage(Uri uri)
	{
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		intent.putExtra("crop", "true");
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		intent.putExtra("outputX", 150);
		intent.putExtra("outputY", 150);
		intent.putExtra("return-data", true);
		startActivityForResult(intent, FROM_CUT);
	}
}
